﻿// FogBugz Community Case List Plugin
// ----------------------------------
// 
// Copyright 2010, 2011 by Sirius Technologies AG / Arsène von Wyss - avw@gmx.ch
// 
// Development has been sponsered by Sirius Technologies AG, Basel
// 
// Source:
// 
// https://communitycaselist.googlecode.com/hg/
// 
// License:
// 
// The library is distributed under the GNU Lesser General Public License:
// http://www.gnu.org/licenses/lgpl.html
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

using FogCreek.FogBugz;
using FogCreek.FogBugz.Plugins.Entity;

namespace Sirius.CommunityCaseList {
	partial class CommunityCaseList {
		private static readonly Random rnd = new Random();

		private static char GetTicketChar() {
			return "0123456789abcdefghijklmnopqrstuv"[rnd.Next(32)];
		}

		private string GenerateTicket(int ixBug) {
			StringBuilder result = new StringBuilder(28);
			result.Append(ixBug);
			result.Append('_');
			for (int i = 0; i < 16; i++) {
				result.Append(GetTicketChar());
			}
			return result.ToString();
		}

		public void BugCommitBefore(CBug bug, BugAction nBugAction, CBugEvent bugevent, bool fPublic) {
			if (!fPublic) {
				bool? useTicket = new bool?();
				switch (api.Request[api.AddPluginPrefix("ticket")]) {
				case "private":
					useTicket = false;
					break;
				case "public":
					useTicket = true;
					break;
				}
				Match match = ticketParser.Match(bug.sTicket ?? string.Empty);
				if (useTicket.HasValue && (useTicket.Value != match.Groups["case"].Success)) {
					if (useTicket.Value) {
						if (match.Groups["long"].Success) {
							bug.sTicket = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", bug.ixBug, match.Groups["long"].Value);
						} else {
							bug.sTicket = GenerateTicket(bug.ixBug);
						}
						if (string.IsNullOrEmpty(bug.sOriginalTitle)) {
							bug.sOriginalTitle = bug.sTitle;
						}
						bugevent.AppendChangeLine(string.Format("Made the case public (ticket {0}).", bug.sTicket));
					} else {
						if (match.Success) {
							bug.sTicket = "removed_"+bug.sTicket;
						} else {
							bug.sTicket = string.Empty;
						}
						bugevent.AppendChangeLine("Made the case private (cleared ticket).");
					}
				}
			}
		}

		public void BugCommitAfter(CBug bug, BugAction nBugAction, CBugEvent bugevent, bool fPublic) {}

		public void BugCommitRollback(CBug bug, BugAction nBugAction, bool fPublic) {}
	}
}
